package org.springblade.tms.module.statementorder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.common.utils.EnumUtils;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.tms.global.enums.TorTypeEnum;
import org.springblade.tms.module.car.vo.CarExcelVO;
import org.springblade.tms.module.statementorder.base.SettlementSheetService;
import org.springblade.tms.module.statementorder.dto.SettlementSheetDTO;
import org.springblade.tms.module.statementorder.entity.TMStatementOrder;
import org.springblade.tms.module.statementorder.service.IStatementOrderService;
import org.springblade.tms.module.statementorder.vo.ChargeDetailsVo;
import org.springblade.tms.module.statementorder.vo.TMStatementOrderExcelVO;
import org.springblade.tms.module.statementorder.vo.TMStatementOrderVO;
import org.springblade.tms.module.statementorder.vo.uploadVoucherVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.statementorder.controller
 * @className: SettlementSheetController
 * @author: yr
 * @description: 结算单接口
 * @date: 2022/3/30 10:09
 */
@RestController
@RequestMapping("statementorder/settlementSheet")
@Api(value = "结算单-yr", tags = "结算单接口-yr")
public class SettlementSheetController {
    @Resource
    private SettlementSheetService settlementSheetService;
    @Resource
    private IStatementOrderService statementOrderService;

    /**
     * @param settlementSheetDTO:
     * @param query:
     * @return R<IPage < TMStatementOrderVO>>
     * @author yr
     * @description 运费结算单列表
     * @date 2022/3/30 11:04
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "运费结算单列表", notes = "传入settlementSheetDTO")
    public R<IPage<TMStatementOrderVO>> list(SettlementSheetDTO settlementSheetDTO, Query query) {
        return settlementSheetService.list(settlementSheetDTO, query);
    }

//	/**
//	 * @param query:
//	 * @return R<IPage < ChargeDetailsVo>>
//	 * @author yr
//	 * @description 运费明细
//	 * @date 2022/4/13 16:56
//	 */
//	@GetMapping("/chargeDetails")
//	@ApiOperationSupport(order = 2)
//	@ApiOperation(value = "运费明细", notes = "传入结算单id")
//	public R<IPage<ChargeDetailsVo>> chargeDetails(Long id,Query query) {
//		return settlementSheetService.chargeDetails(id,query);
//	}

    @PostMapping("/confirmShipping")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "确认运费", notes = "传入ids")
    public R<Boolean> confirmShipping(@RequestBody String ids) {
        Map<String, String> idMap = JsonUtil.parse(ids, Map.class);
        return settlementSheetService.confirmShipping(idMap.get("ids"));
    }

    @PostMapping("/staging")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "暂存", notes = "传入ids")
    public R<Boolean> staging(@RequestBody String ids) {
        Map<String, String> idMap = JsonUtil.parse(ids, Map.class);
        return settlementSheetService.staging(idMap.get("ids"));
    }

    @PostMapping("/uploadCertificate")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "上传凭证", notes = "传入uploadVoucherVo")
    public R<Boolean> uploadCertificate(@RequestBody uploadVoucherVo uploadVoucherVo) {
        TMStatementOrder statementOrder = statementOrderService.getById(uploadVoucherVo.getId());
        statementOrder.setUploadVoucher(uploadVoucherVo.getUploadVoucher());
        boolean update = statementOrderService.updateById(statementOrder);
        return R.status(update);
    }

    /**
     * @return R<IPage < TMStatementOrderVO>>
     * @author yr
     * @description 运费结算单列表
     * @date 2022/3/30 11:04
     */
    @GetMapping("/export")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "运输结算单列表导出")
    public R<Boolean> export(HttpServletResponse response,SettlementSheetDTO settlementSheetDTO) {
        List<TMStatementOrderVO> statementOrderVO = settlementSheetService.export(settlementSheetDTO);
        List<TMStatementOrderExcelVO> tmStatementOrderExcelVOS = BeanUtil.copy(statementOrderVO, TMStatementOrderExcelVO.class);
        tmStatementOrderExcelVOS.forEach(tmStatementOrderExcelVO -> {
            //修改货运订单类型为货运订单类型描述
            Optional<TorTypeEnum> enumByValue = EnumUtils.getEnumByValue(TorTypeEnum.class, tmStatementOrderExcelVO.getFreightOrderType());
            if (enumByValue != null) {
                tmStatementOrderExcelVO.setFreightOrderType(enumByValue.get().description());
            }
        });
        ExcelUtil.export(response, "运输结算单", "运输结算单", tmStatementOrderExcelVOS, TMStatementOrderExcelVO.class);
        return R.data(true);
    }
}
